<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\11\28 0028
 * Time: 9:30
 */
namespace demo\model;
class Mongo
{
    private $config;
    private $client;

    public function __construct()
    {
        //mongodb://127.0.0.1:27017/admin:admin
        $this->config = "mongodb://127.0.0.1:27017";
        $this->client = new MongoDB\Driver\Manager($this->config);
    }

    public function add($document, $namespace)
    {
        $bulk = new MongoDB\Driver\BulkWrite;
        //$document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'content_id', 'id' => 1];

        $bulk->insert($document);

        $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
        $result = $this->client->executeBulkWrite($namespace, $bulk, $writeConcern);
    }

    public function update()
    {

    }

    public function delete()
    {

    }

    public function select($filter, $options, $namespace)
    {
        $query = new MongoDB\Driver\Query($filter, $options);
        $cursor = $this->client->executeQuery($namespace, $query);

        foreach ($cursor as $document) {
            print_r($document);
        }
    }

    public function findOne()
    {

    }
}